package com.tpc.yaoxpt.service;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.tpc.yaoxpt.main.LogModule;
import com.tpc.yaoxpt.main.MainController;
import com.tpc.yaoxpt.utils.*;
import javafx.application.Platform;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.sql.Connection;
import java.util.Date;

@Slf4j
public class GoodsService {
    final LogModule logModule = LogModule.getInstance();

    public void getGoodsInfo(){

        String intType = "C003";
        String intName = "获取商品信息";
        JSONObject jsonObject = new JSONObject();
        Integer pageNumber = Integer.valueOf(FileUtil.getValue(intType, "PAGENUMBER"));
        String url = FileUtil.getValue("PTCONFIG","URL");
        String month = FileUtil.getValue(intType,"MONTH");

        url = url + "v2/delv/drug/prod/page";
        jsonObject.put("month",month);
        jsonObject.put("currentPageNumber", pageNumber );

//        logModule.appendLogInfo( "\n" + StringUtil.getInString(intType,intName,jsonObject.toString()));
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                MainController.staticLogText.appendText("\n"+StringUtil.getInString(intType,intName,jsonObject.toString()));
            }
        });
//        log.info(  "业务请求参数："+StringUtil.getInString(intType,intName,jsonObject.toString()));
        String returnInfo = HttpClientUtil.doPost(url, jsonObject);
//        logModule.appendLogInfo( "\n" + StringUtil.getOutString(intType,intName,returnInfo));
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                MainController.staticLogText.appendText("\n"+StringUtil.getOutString(intType,intName,returnInfo));
            }
        });
        log.info("业务返回参数："+StringUtil.getOutString(intType,intName,returnInfo));
        JSONObject outJSON = JSONObject.parseObject(returnInfo);
        if(outJSON.getInteger("returnCode")==1){
            if(outJSON.getInteger("currentPageNumber")<=outJSON.getInteger("totalPageCount")){
                Connection connection = null ;
                try {

                    connection = JDBCUtil.getConnection();
                    TableSaveUtil.tableSave(connection,FileUtil.getValue(intType,"TABLE"),outJSON.getJSONArray("dataList"));
                    FileUtil.addSection(intType,"PAGENUMBER", String.valueOf(pageNumber+1));
                } catch (IOException e) {
//                    logModule.appendLogInfo( "\n" + StringUtil.getErrorString(intType,intName,e.getMessage()));
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            MainController.staticLogText.appendText("\n"+StringUtil.getErrorString(intType,intName,e.getMessage()));
                        }
                    });
                    log.info("数据异常错误："+StringUtil.getErrorString(intType,intName,e.getMessage()));
                    throw new RuntimeException(e);
                } finally {
                  JDBCUtil.close(connection);
                }
            }else{
                try {
                    Date date = DateUtil.parse(month,DatePattern.NORM_MONTH_PATTERN);
                    Date newDate = DateUtil.offsetMonth(date, 1);
                    Date nowDate = new Date();
                    boolean isAfterNow = DateUtil.compare(newDate, nowDate) > 0;
                    if(isAfterNow){
                        FileUtil.addSection(intType,"MONTH", DateUtil.format(new Date(), DatePattern.NORM_MONTH_PATTERN));
                    }else{
                        String newDateStr = DateUtil.format(newDate,DatePattern.NORM_MONTH_PATTERN);
                        FileUtil.addSection(intType,"MONTH", newDateStr);
                    }
                    FileUtil.addSection(intType,"PAGENUMBER", String.valueOf(1));
                } catch (IOException e) {
//                    logModule.appendLogInfo( "\n" + StringUtil.getErrorString(intType,intName,e.getMessage()));
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            MainController.staticLogText.appendText("\n"+StringUtil.getErrorString(intType,intName,e.getMessage()));
                        }
                    });
                    log.info("数据异常错误："+StringUtil.getErrorString(intType,intName,e.getMessage()));
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
